package system.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;


import system.StaticData;

public final class SystemMainFilter implements Filter {
	private static final String ENCODING = "UTF-8";

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		// 防止站外使用本系统
		if (StaticData.SYSYTEM_DENY_OUTSIDE_CONNECTION) {
			System.out.println("启动REFERER检查");
			String REFERER = ((HttpServletRequest)request).getHeader("referer");
			if (REFERER == null || REFERER.indexOf(StaticData.WEB_ADDRESS) != 0){
				System.out.println("非法REFERER：" + REFERER);
				return;
			}
				
		}
		
		
		request.setCharacterEncoding(ENCODING);
		//System.out.println(request.getCharacterEncoding());

		response.setCharacterEncoding(ENCODING);
		chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig config) throws ServletException {
	}
	
	@Override
	public void destroy() {
	}

}
